{
    function ajax(options) {
        let xhr = new XMLHttpRequest()
    
        // 设置默认值
        options.type = options.type || 'get'
        options.async = options.async || true
        options.header = options.header || {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    
        // console.log(options.type)
        let arr = []
    
        for (let attr in options.data) {
            arr.push(attr + '=' + options.data[attr])
        }
    
        // console.log(arr)
        let str = arr.join('&')
        // console.log(str)
    
        // 判断是post还是get
        if (options.type === 'get') {
            xhr.open('get', options.url + '?' + str, options.async)
            xhr.send()
        } else {
            xhr.open('post', options.url, options.async)
            // 设置请求头
            for (let attr in options.header) {
                xhr.setRequestHeader(attr, options.header[attr])
            }
            // xhr.setRequestHeader('Content-Type', 'application/json')
            xhr.send(str)
        }
    
    
        xhr.onload = function () {
            // 后台来的所有数据都是字符串
            // console.log(xhr.responseText)
            // let obj = JSON.parse(xhr.responseText)
            // console.log(obj)
    
            // 把拿到的数据返回给 调用函数的人
            // 回调函数
            if (options.success) {
                options.success(xhr.responseText)
            }
        }
    }

    var code = "";
    $.fn.code_Obj = function(o) {
        var _this = $(this);
        var options = {
            code_l: o.codeLength,//验证码长度
            codeChars: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            ],
            codeColors: ['#f44336', '#009688', '#cddc39', '#03a9f4', '#9c27b0', '#5e4444', '#9ebf9f', '#ffc8c4', '#2b4754', '#b4ced9', '#835f53', '#aa677e'],
            code_Init: function() {
                code = "";
                var codeColor = "";
                var checkCode = _this.find("#data_code");
                for(var i = 0; i < this.code_l; i++) {
                    var charNum = Math.floor(Math.random() * 52);
                    code += this.codeChars[charNum];
                }
                for(var i = 0; i < this.codeColors.length; i++) {
                    var charNum = Math.floor(Math.random() * 12);
                    codeColor = this.codeColors[charNum];
                }
                //在这里将验证码输出
                console.log(code);
                if(checkCode) {
                    checkCode.css('color', codeColor);
                    checkCode.className = "code";
                    checkCode.text(code);
                    checkCode.attr('data-value', code);
                }
            }
        };

        options.code_Init();//初始化验证码
        _this.find("#data_code").bind('click', function() {
            options.code_Init();
            
        });
    };
          /**
	    		 * 验证码
	    		 * codeLength:验证码长度
	    		 */
           $('#check-code').code_Obj({
            codeLength: 5
        });
        
        $("#data_code").on('click', function() {
            arr[1].re=code
            
        });
        //创建数据供测试使用
        let arr=[
        {
            err:'iconfont icon-tishi',
            ok:'iconfont icon-dianzan', 
            flag:'false',
            re:/^1[3-9]\d{9}$/
        },
        {
            err:'iconfont icon-tishi',
            ok:'iconfont icon-dianzan', 
            flag:'false',
            re:code
        },
        {
            err:'iconfont icon-tishi',
            ok:'iconfont icon-dianzan', 
            flag:'false',
            re:/^[0-9a-zA-Z]{4}$/
        },
        {
            err:'iconfont icon-tishi',
            ok:'iconfont icon-dianzan', 
            flag:'false',
            re:/^\w{6,16}$/
        },
        {
            err:'iconfont icon-tishi',
            ok:'iconfont icon-dianzan', 
            flag:'false'
        },
       
    ]
    //获取注册按钮
    let oReg=document.querySelector('.register')
    //获取手机号的验证框
    let oPhone=document.querySelector('.phone')
    //获取验证码
    let oVerifyCode=document.querySelector('.verifyCode')
    let oTestCode=document.querySelector('.testCode')
    let oYanZhengM=document.querySelector('#yanZhengM')
    let oPassword=document.querySelector('.password')
    let oPasswordOk=document.querySelector('.passwordOk')
    let oUserCheck=document.querySelector('.userCheck')
 
  
    oPhone.onfocus=function(){
        //当前框下面的第二个兄弟节点
        let nextt=this.nextElementSibling.nextElementSibling
        nextt.style.display='block'
        nextt.innerHTML='请输入十一位手机号'
    }
    oPhone.onblur=function(){
        let next=this.nextElementSibling.nextElementSibling
        let nextt=this.nextElementSibling
        this.nextElementSibling.style.display='block'
        if(arr[0].re.test(this.value)){
            arr[0].flag=true
            next.style.display='none'
            this.nextElementSibling.className=arr[0].ok
        }else{
            next.innerHTML='输入正确的手机号码'
            /* nextt.style.display='none' */
            next.style.background='red'
            this.nextElementSibling.className=arr[0].err
            arr[0].flag=false
        }
    }
//-----------------------------------------------------------------------------
    oVerifyCode.onfocus=function(){
        //当前框下面的第二个兄弟节点
        let nextt=this.nextElementSibling.nextElementSibling
        nextt.style.display='block'
        nextt.innerHTML='请输入验证码'
    }
    oVerifyCode.onblur=function(){
        let next=this.nextElementSibling.nextElementSibling
        let nextt=this.nextElementSibling
        this.nextElementSibling.style.display='block'
        if(arr[1].re===this.value){
            arr[1].flag=true
            next.style.display='none'
            this.nextElementSibling.className=arr[0].ok
        }else{
            next.innerHTML='输入正确的验证码'
            /* nextt.style.display='none' */
            next.style.background='red'
            this.nextElementSibling.className=arr[0].err
            arr[1].flag=false
        }
    }
    //在这里是获取手机验证码
    oYanZhengM.onclick=function(){
      
        console.log(arr)
        //循环arr判断里面的true和false
        for(let i=0;i<2;i++){
            if(arr[i].flag===false){
                return
            }

        } 
        //点击之后就会禁止按钮 不让点击
        this.disabled=true
        let miao=5
        let timer=setInterval(()=>{
          
            miao--
            this.value=miao+'秒后重新获取'
            if(miao===0){
                clearInterval(timer)
                //因为这里使用的是箭头函数 所以this指向外面点击事件的发起者
                this.value='获取验证码'
                this.disabled=false
            }
        },1000)
       
    }
    oTestCode.onblur=function(){
        
        let next=this.nextElementSibling.nextElementSibling
        let nextt=this.nextElementSibling
        this.nextElementSibling.style.display='block'
        if(arr[2].re.test(this.value)){
            arr[2].flag=true
            next.style.display='none'
            this.nextElementSibling.className=arr[0].ok
        }else{
            next.innerHTML='输入正确的验证码'
            next.style.display='block'
            next.style.background='red'
            this.nextElementSibling.className=arr[0].err
            arr[2].flag=false
        }
    }


    oPassword.onfocus=function(){
        //当前框下面的第二个兄弟节点
        let nextt=this.nextElementSibling.nextElementSibling
        nextt.style.display='block'
        nextt.innerHTML='英文/大写英文/数字'
    }
    oPassword.onblur=function(){
        let next=this.nextElementSibling.nextElementSibling
        let nextt=this.nextElementSibling
        this.nextElementSibling.style.display='block'
        if(arr[3].re.test(this.value)){
            arr[3].flag=true
            next.style.display='none'
            this.nextElementSibling.className=arr[3].ok
        }else{
            next.innerHTML='您的密码不安全'
            /* nextt.style.display='none' */
            next.style.background='red'
            this.nextElementSibling.className=arr[3].err
            arr[3].flag=false
        }
    }

    oPasswordOk.onblur=function(){
        let next=this.nextElementSibling.nextElementSibling
        let nextt=this.nextElementSibling
        this.nextElementSibling.style.display='block'
        if(this.value===oPassword.value && oPassword.value!==''){
            arr[3].flag=true
            next.style.display='none'
            this.nextElementSibling.className=arr[3].ok
        }else{
            next.innerHTML='两次密码不一致'
            next.style.display='block'
            next.style.background='red'
            this.nextElementSibling.className=arr[3].err
            arr[3].flag=false
        }
    }
    oUserCheck.onchange=function(){
        if(this.checked===true){
            arr[4].flag=true
        }else{
            arr[4].flag=false
        }
    }
    oReg.onclick=function(){
        //当点击按钮的时候 开始循环数据
        for(let i=0;i<arr.length;i++){
            if(arr[i].flag===false){
                return
            }
        }
        alert('可以执行ajax代码了')
        ajax({
            url:'../php/zhuce.php',
            type:'post',
            data:{
                username:oPhone.value,
                
                password:oPassword.value,
            },
            success(res){
                let obj=JSON.parse(res)
                console.log(obj)
                location.href='../html/register.htm'
                console.log('没有跳转')
            }
        })

       
    }
}